package com.xiaoshuidi.cloud.module.bpm.framework.bpm.core.event;

import com.alibaba.nacos.shaded.com.google.common.primitives.Longs;
import com.xiaoshuidi.cloud.module.bpm.mq.message.BpmProcessInstanceResultEvent;
import lombok.AllArgsConstructor;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.cloud.bus.ServiceMatcher;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.validation.annotation.Validated;

/**
 * {@link BpmProcessInstanceResultEvent} 的生产者
 *
 * @author 
 */
@AllArgsConstructor
@Validated
public class BpmProcessInstanceResultEventPublisher {

    private final ApplicationEventPublisher publisher;
    private final ServiceMatcher serviceMatcher;

    public void sendProcessInstanceResultEvent(HistoricProcessInstance processInstance, Integer result) {
        Object destinationService = processInstance.getProcessVariables().get("destinationService");
        Object businessType = processInstance.getProcessVariables().get("businessType");
        Object processDefinitionCategory = processInstance.getProcessVariables().get("processDefinitionCategory");
        BpmProcessInstanceResultEvent event = new BpmProcessInstanceResultEvent(this, serviceMatcher.getBusId(), destinationService == null ? "unknown" : destinationService.toString());
        event.setId(processInstance.getId());
        event.setProcessDefinitionKey(processInstance.getProcessDefinitionKey());
        event.setProcessDefinitionCategory(processDefinitionCategory == null ? "" : processDefinitionCategory.toString());
        event.setBusinessKey(processInstance.getBusinessKey());
        event.setBusinessType(businessType == null ? "unknown" : businessType.toString());
        event.setResult(result);
        event.setTenantId(Longs.tryParse(processInstance.getTenantId()));
        publisher.publishEvent(event);
    }

    public void sendProcessInstanceResultEvent(ProcessInstance processInstance, Integer result) {
        Object destinationService = processInstance.getProcessVariables().get("destinationService");
        Object businessType = processInstance.getProcessVariables().get("businessType");
        Object processDefinitionCategory = processInstance.getProcessVariables().get("processDefinitionCategory");
        BpmProcessInstanceResultEvent event = new BpmProcessInstanceResultEvent(this, serviceMatcher.getBusId(), destinationService == null ? "unknown" : destinationService.toString());
        event.setId(processInstance.getId());
        event.setProcessDefinitionKey(processInstance.getProcessDefinitionKey());
        event.setProcessDefinitionCategory(processDefinitionCategory == null ? "" : processDefinitionCategory.toString());
        event.setBusinessKey(processInstance.getBusinessKey());
        event.setBusinessType(businessType == null ? "unknown" : businessType.toString());
        event.setResult(result);
        event.setTenantId(Longs.tryParse(processInstance.getTenantId()));
        publisher.publishEvent(event);
    }
}
